package com.zky.gulimall.product;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @Description TODO
 * @Author 张开源
 * @Date 2022/11/12 15:18
 * @Version 1.0
 **/
public class ABADemo {
    public static void main(String[] args) {
        Book book = new Book(1, "javaBook");
        AtomicStampedReference<Book> reference = new AtomicStampedReference<Book>(book, 1);
        System.out.println(reference.getReference() + "\t" + reference.getStamp());

        Book book2 = new Book(2, "mysqlBook");
        boolean b = reference.compareAndSet(book, book2, reference.getStamp(), reference.getStamp() + 1);
        System.out.println(b + "\t" + reference.getReference() + "\t" + reference.getStamp());


        boolean b1 = reference.compareAndSet(book2, book, reference.getStamp(), reference.getStamp() + 1);
        System.out.println(b1 + "\t" + reference.getReference() + "\t" + reference.getStamp());
    }


    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    static
    class Book {
        private int id;
        private String bookName;
    }
}
